package com.nf.health.app.service;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.models.CityAreaBean;
import com.nf.health.app.utils.AssetsUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 获取本地数据的工具类
 * eg：读取assets下的文件里的内容
 * @author yyh
 */
public class ServiceData  {
    /**
     * 获取城市列表
     * @param cnt
     * @return
     */
    public static   List<CityAreaBean> getMyCityArea(Context cnt){
        String cityArea = AssetsUtils.readAssets(cnt,"city.json");
        //HashMap<String,List<String>> hashMap = new HashMap<>();
        List<CityAreaBean> list = null;
        Gson gson = new MyGsonBuilder().createGson();
        list = gson.fromJson(cityArea, new TypeToken<List<CityAreaBean>>() {
        }.getType());
        return list;
    }
    public static LinkedHashMap<String,LinkedList<String>> getMyCityArea2(Context cnt){
        LinkedHashMap<String,LinkedList<String>> linkedListLinkedHashMap = new LinkedHashMap<>();
        linkedListLinkedHashMap.put("全国",new LinkedList<String>());
        String cityArea = AssetsUtils.readAssets(cnt, "city.json");
        try {
            JSONArray json = new JSONArray(cityArea);
            int length = json.length();
            for(int i = 0 ; i < length;i++){
                JSONObject jsonObject = json.optJSONObject(i);
                String cityName = jsonObject.optString("name");
                if(!TextUtils.isEmpty(cityName)){
                    JSONArray subArray1 = jsonObject.optJSONArray("area");
                    LinkedList<String> subList = new LinkedList<>();
                    for (int l = 0; l < subArray1.length(); l++) {
                        JSONObject subJsonObject = subArray1.optJSONObject(l);
                        String subCityName = subJsonObject.optString("name");
                        subList.add(subCityName);
                    }
                    linkedListLinkedHashMap.put(cityName,subList);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return linkedListLinkedHashMap;
    }
    public static int getCityDefaultPosition(LinkedHashMap<String,LinkedList<String>> list,String defaultCity){
        int result = -1;
        if(list == null){
            result = 0;
            return result;
        }
        if(TextUtils.isEmpty(defaultCity)){
            result = 1;
            return result;
        }
        Set<Map.Entry<String, LinkedList<String>>> entrySet = list.entrySet();
        for(Map.Entry<String,LinkedList<String>> map: entrySet){
            result++;
            if(defaultCity.contains(map.getKey())){
                    break;
            }
        }
        return result;
    }
    public static String getCityDefaultPosition2(LinkedHashMap<String,LinkedList<String>> list,String locationArea){
        int position = -1;
        String result = "";
        if(list == null){
            result = "";
            return result;
        }
        if(TextUtils.isEmpty(locationArea)){
            result = "";
            return result;
        }
        Set<Map.Entry<String, LinkedList<String>>> entrySet = list.entrySet();
        for(Map.Entry<String,LinkedList<String>> map: entrySet){
            if(locationArea.contains(map.getKey())){
                LinkedList<String> linkedList  = map.getValue();
                for(int i = 0 ; i < linkedList.size(); i++){
                    position++;
                    if(locationArea.contains(linkedList.get(i))){
                        result = map.getKey()+"|"+linkedList.get(i)+"|"+position;
                        break;
                    }
                }
                    break;
            }
        }
        return result;
    }
    public static LinkedHashMap<String,LinkedList<String>>  getMyOfferData(Context cnt){
        LinkedHashMap<String,LinkedList<String>> linkedListLinkedHashMap = new LinkedHashMap<>();
        String cityArea = AssetsUtils.readAssets(cnt,"office.json");
        try {
            JSONArray json = new JSONArray(cityArea);
            int length = json.length();
            for(int i = 0 ; i < length;i++){
                JSONObject jsonObject = json.optJSONObject(i);
                String cityName = jsonObject.optString("name");
                if(!TextUtils.isEmpty(cityName)){
                    JSONArray subArray1 = jsonObject.optJSONArray("office");
                    LinkedList<String> subList = new LinkedList<>();
                    for (int l = 0; l < subArray1.length(); l++) {
                        JSONObject subJsonObject = subArray1.optJSONObject(l);
                        String subCityName = subJsonObject.optString("name");
                        subList.add(subCityName);
                    }
                    linkedListLinkedHashMap.put(cityName,subList);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return linkedListLinkedHashMap;
    }
}
